/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.cacheplugins  
 */
package com.apache.bid.service.taskplugins;

import java.util.ArrayList;
import java.util.List;

import com.apache.api.api.TimeTaskService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.entity.BidOrg;
import com.apache.bid.manager.BidOrgManager;
import com.apache.cache.util.Validator;

/**
 * description:  添加/删除组织方缓存插件(每天执行一次任务
 * @author Hou Dayu 创建时间：2016-5-5  
 * 
 */
public class BidOrgCachePluginTask implements PluginConnector, TimeTaskService {

	private transient static boolean SaveOrDelOrgTaskMark = true;

	private BidOrgManager bidOrgManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.api.TimeTaskService#run()  
	 */
	@SuppressWarnings("rawtypes")
	public boolean run() {
		try {
			execute(new ParamsVo());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		if (Validator.isEmpty(param.getParams())) {
			try {
				if (SaveOrDelOrgTaskMark) {
					SaveOrDelOrgTaskMark = false;
					ParamsVo<BidOrg> vo = new ParamsVo<BidOrg>();
					vo.setKey("findOrgEnames");
					List<String> obj = (List<String>) bidOrgManager.execute(vo);
					if (Validator.isEmpty(obj)) {
						obj = new ArrayList<String>();
					}
					BidCacheFactory.getInstance().saveBidOrgEnames(obj);
				}
			} finally {
				SaveOrDelOrgTaskMark = true;
			}
		} else {
			String ifSave = String.valueOf(param.getParams("ifSave"));
			List<String> names = BidCacheFactory.getInstance().getBidOrgEnames();
			String orgEname = String.valueOf(param.getParams("orgEname"));
			if ("true".equalsIgnoreCase(ifSave)) {
				names.add(orgEname);
			} else {
				names.remove(orgEname);
			}
			BidCacheFactory.getInstance().saveBidOrgEnames(names);
		}
		return entity;
	}

	public void setBidOrgManager(BidOrgManager bidOrgManager) {
		this.bidOrgManager = bidOrgManager;
	}
}
